<?php
namespace Home\Controller;
use Think\Controller;
class ApiController extends Controller {
    public function index(){
		if (isset($_GET['echostr'])) {
		    $this->valid();
		}else{
		    $this->responseMsg();
		}
    }
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature()
    {
		define("TOKEN", "weixin");
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }

    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $fromUsername = $postObj->FromUserName;
            $toUsername = $postObj->ToUserName;
            $keyword = trim($postObj->Content);
            $msgType = $postObj->MsgType;
			$event = $postObj->Event;
			$eventkey = $postObj->EventKey;
            $time = time();
            $textTpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                        <FuncFlag>0</FuncFlag>
                        </xml>";
            if($keyword == "?" || $keyword == "？")
            {
				$token = A("Token");
				$access_token = $token->index();
                $msgType = "text";
                $contentStr = '独立的方法进行被动回复！！~~';
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
				echo $resultStr;
				
				$contentText = '独立的方法进行客服接口回复~~';
				$contentText = urlencode($contentText);//转码
				$a = array("content"=>"{$contentText}");
				$b = array("touser"=>"{$fromUsername}","msgtype"=>"text","text"=>$a);
				$post = json_encode($b);
				$post = urldecode($post);//解码
				$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$access_token";
				$this->https_request($url,$post);
				$this->https_request($url,$post);
				
            }
			if($msgType == "event" and $event == "CLICK"){
				$fault = A("Fault");
				$fault->fault_push($fromUsername);
			}
        }else{
            echo "";
            exit;
        }
    }
	public function https_request($url,$data){
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
		curl_setopt($ch,CURLOPT_POST,1);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_exec($ch);
		curl_close($ch);
	}
}